// declare the namespace
syslib.namespace("syslib.ui.tree");

// class structure
syslib.ui.tree.TreeNode = __class(syslib.ui.UIComponent, {
	__componentName : "syslib.ui.tree.TreeNode", 
	
	options : {}, 
	childContainer : null, 
	labelContainer : null, 
	
	__construct : function(label, options){
		// extend the options
		this.options = __extend({
			id : syslib.element.generateID(), 
			label : label, 
			parent : null
		}, options);
		
		// load the css
		syslib.loadStyleForComponent("tree");
		
		// update the containers
		this.childContainer = null;
		this.labelContainer = null;
	}, 
	
	__createElement : function(){
		// create the element
		var container 		= document.createElement("div");
		var labelContainer	= document.createElement("div");
		var childContainer	= document.createElement("div");
		
		// update the style
		labelContainer.className = "syslib-text";
		childContainer.className = "syslib-treenode-child-container";
		
		// update the data
		labelContainer.innerHTML = this.options.label;
		
		// append
		container.appendChild(labelContainer);
		container.appendChild(childContainer);
		
		// set the containers
		this.childContainer = childContainer;
		this.labelContainer = labelContainer;
		
		// set the element and return
		this.__element = container;
		return container;
	}, 
	
	appendChild : function(treeNode){
		if(!__valid(treeNode.__componentName) || treeNode.__componentName != this.__componentName){
			return false;
		} else {
			this.childContainer.appendChild(treeNode.getElement());
		}
	}, 
	
	appendTo : function(container){
		if(__valid(container) && container.__componentName === this.__componentName){
			// append the node into another node
			container.appendChild(this);
		} else {
			// get the parent appender function
			var append = this.__super.appendTo.applyTo(this);
			append(container);
		}
	}
});

// tag conversor
syslib.ui.tree.TreeNodeTag = __class(syslib.ui.TagConversor, {
	__parentTagName : "treenode",
	
	treeNode : null,
	
	execute : function(){
		// create the button
		var treeNode = new syslib.ui.tree.TreeNode(this.__attributes.label);
		
		// store the tree node
		this.treeNode = treeNode;
		
		// append the button
		treeNode.appendTo(this.__container);
	}, 
	
	getComponent : function(){
		return this.treeNode;
	}
});